define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'sport/batch/index' + location.search,
                    add_url: 'sport/batch/add',
                    // edit_url: 'sport/batch/edit',
                    del_url: 'sport/batch/del',
                    multi_url: 'sport/batch/multi',
                    import_url: 'sport/batch/import',
                    table: 'sport_batch',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user.nickname', title: __('用户昵称'), operate: false},
                        {field: 'user.mobile', title: __('用户手机号')},
                        {field: 'area.area_name', title: __('地区'), operate: false},
                        // {field: 'score_type', title: __('Score_type'), searchList: {"middle":__('Score_type middle'),"senior":__('Score_type senior')}, formatter: Table.api.formatter.normal},
                        {field: 'score_header', title: __('Score_header'), operate: 'LIKE'},
                        {field: 'task_id', title: __('Task_id'), operate: 'LIKE'},
                        {field: 'over_time', title: __('Over_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'source_filename', title: __('待批量计算文件'), operate: false, 
                          formatter: function (value, row, index) {
                            if (value != '') {
                              return '<a href="'+value+'" class="btn btn-success" title="下载"><i class="fa fa-download"></i> 下载</a>';
                            }
                            return '-'
                          }
                        },
                        {field: 'filename', title: __('已完成批量计算文件'), operate: false,
                          formatter: function (value, row, index) {
                            if (value != null) {
                              return '<a href="'+value+'" class="btn btn-success" title="下载"><i class="fa fa-download"></i> 下载</a>';
                            }
                            return '-'
                          }
                        },
                        {field: 'total_records', title: __('Total_records')},
                        {field: 'success_records', title: __('Success_records')},
                        {field: 'fail_records', title: __('Fail_records')},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
                showToggle: false,
                showColumns: false,
                showExport: false,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
